<?php
/**
 * 车主活动控制器
 *
 * @package    app
 * @version    1.0
 * @author     Ray 33705910@qq.com
 * @license    MIT License
 * @copyright  2013 - 2015 Ray
 * @link       http://wangxiaolei.cn
 */

/**
 * 本控制器主要用于：
 * 1.
 * @package  app
 * @extends  Controller
 */
namespace admin;

class Controller_Activity extends Controller_BaseController {


    public function before(){
        parent::before();

        $params = array(
            'controller_name' => '车主活动管理'
        );
        \View::set_global($params);
    }

    /**
    * 活动列表
    *
    * @param $cid 活动类目ID
    * @return
    */
    public function action_index(){
        $params = array(
            'title' => '活动管理',
            'menu' => 'activity',
            'action_name' => '活动列表'
        );


        $items = \Model_Activity::query()
            ->where('is_delete', 0);

        //分页查询
        $count = $items->count();
        $config = array(
            'pagination_url' => "/admin/activity",
            'total_items'    => $count,
            'per_page'       => \Input::get('count', 10),
            'uri_segment'    => 'start',
            'show_first'     => true,
            'show_last'      => true,
            'name'           => 'bootstrap3_cn' . (\Input::is_ajax() ? '_ajax' : '')
        );

        $pagination = new \Pagination($config);
        $params['pagination'] = $pagination;        
        $params['items'] = $items
                            ->rows_offset($pagination->offset)
                            ->rows_limit($pagination->per_page)
                            ->get();
        if(\Input::is_ajax()){
            $items = array();
            foreach ($params['items'] as $key => $value) {
                $item = $value->to_array();
                $item['created_at'] = date('Y-m-d H:i:s', $value->created_at);
                $item['author'] = $value->author->to_array();
                $item['category'] = $value->category->to_array();
                array_push($items, $item);
            }
            die(json_encode(array('status' => 'succ', 'msg' => '', 'errcode' => 0, 'data' => $items, 'pagination' => htmlspecialchars_decode($pagination))));
        }

        \View::set_global($params);
        $this->template->content = \View::forge("ace/activity/index");
    }

  	/**
  	* 新增一条活动
  	*
    * @param $id 活动ID
  	* @return
  	*/
  	public function action_save($id = 0){
        $params = array(
            'title' => ($id ? '编辑' : '新增') . '活动',
            'menu' => "activity-details",
            'action_name' => ($id ? '编辑' : '新增') . '活动'
        );

        $node = false;
        if($id){
            $activity = \Model_Activity::find($id);
        }

    	if(\Input::method() == 'POST'){
            $data = \Input::post();
            $val = \Validation::forge('MyRules');
            $val->add_callable('MyRules');
            $val->add_field( 'title', '活动标题', 'required');
            $val->add_field( 'body', '活动详情', 'required');

            if (! $val->run()){
                foreach ($val->error() as $key => $value) {
                    $errors[$key] = (string)$value;
                }
                if(\Input::is_ajax()){
                    die(json_encode(array('status' => 'err', 'msg' => '表单验证错误', 'data' => $errors, 'errcode' => 10)));
                }
                $msg = array('status' => 'err', 'msg' => '表单验证错误', 'data' => $errors, 'errcode' => 10);
            }else{
                $data['reg_at'] = strtotime($data['reg_at']);
                $data['start_at'] = strtotime($data['start_at']);
                $data['end_at'] = strtotime($data['end_at']);
                if($activity){
                    $activity->set($data);
                }else{
                    $activity = \Model_Activity::forge($data);
                }


                if($activity->save()){
                    $msg = array('status' => 'succ', 'msg' => '操作成功', 'errcode' => 0);
                }else{
                    $msg = array('status' => 'err', 'msg' => '操作失败', 'errcode' => 20);
                    
                }
                if(\Input::is_ajax()){
                    die(json_encode($msg));
                }
                \Session::set_flash('msg', $msg);
            }
            
            
    	}

        if($activity){
            $params['item'] = $activity;
        }

        \View::set_global($params);
        $this->template->content = \View::forge("ace/activity/details");
  	}

  	/**
  	* 删除一条活动
  	*
  	* @param $id int 活动ID
  	* @return
  	*/
  	public function action_delete($id){
        if( ! $id){
            die('无效的参数');
        }

        $node = \Model_Activity::find($id);
        if( ! $node){
            die('无效的参数');
        }

        $node->is_delete = 1;
        if($node->save()){
            if(\Input::is_ajax()){
                die(json_encode(array('stauts' => 'succ', 'msg' => '操作成功', 'errcode' => 0)));
            }
            \Response::redirect("/admin/activity");
        }
        if(\Input::is_ajax()){
            die(json_encode(array('stauts' => 'err', 'msg' => '操作失败', 'errcode' => 20)));
        }
  	}

}
